package com.ww.editsms.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chinawangw.R;
import com.ww.editsms.SmsActivity;
import com.ww.editsms.entity.SMS;
import com.ww.editsms.util.SmsUtil;


public class SMSAdapter extends BaseAdapter {
	Activity context;
	List<SMS> list;
	boolean bool;
	LayoutInflater lif;
	String num;
	public List<SMS> getList() {
		return list;
	}

	public void setList(List<SMS> list) {
		this.list = list;
	}

	public SMSAdapter(Activity context,String num, List<SMS> list, boolean bool) {
		this.context = context;
		this.num = num;
		this.list = list;
		this.bool = bool;
	}

	/**
	 * list总数
	 */
	public int getCount() {
		return list.size();
	}

	/**
	 * 返回 position�?
	 */
	public Object getItem(int position) {
		return null;
	}

	/**
	 * 返回itemId
	 */
	public long getItemId(int position) {
		return position;
	}

	/**
	 * View关键代码
	 */
	public View getView(final int position, View convertView, ViewGroup parent) {
		final MyView mv;
		if (convertView == null) {
			mv = new MyView();
			lif = LayoutInflater.from(context);// 转化到context这个容器
			convertView = lif.inflate(R.layout.sms, null);// 设置要转化的layout文件
		} else {
			mv = (MyView) convertView.getTag();
		}
		mv.linearLayout = (LinearLayout) convertView
				.findViewById(R.id.smsdetails);// 取得实例
		mv.time = (TextView) convertView.findViewById(R.id.itemtime);// 取得实例
		mv.content = (TextView) convertView.findViewById(R.id.content);// 取得实例

		try {
			String time = SmsUtil.getdays(Long.parseLong(list.get(position)
					.getTime()));
			mv.time.setText(time);
		} catch (Exception e) {
			mv.time.setText("陌生人");
		}
		mv.content.setText(list.get(position).getBody());

		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		if (list.get(position).getFilter() == 1 && bool) {
			if (list.get(position).getType() == 1) {
				mv.linearLayout.setBackgroundResource(R.drawable.bg_out_circle_white_normalw);
				lp.gravity = Gravity.LEFT;
			} else if (list.get(position).getType() == 2) {
				mv.linearLayout.setBackgroundResource(R.drawable.bg_out_circle_white_normal);
				lp.gravity = Gravity.RIGHT;
			}
		}else{
			if (list.get(position).getType() == 1) {
				mv.linearLayout.setBackgroundResource(R.drawable.bg_in_circle_brown_normal);
				lp.gravity = Gravity.LEFT;
			} else if (list.get(position).getType() == 2) {
				mv.linearLayout.setBackgroundResource(R.drawable.bg_out_circle_green_normal);
				lp.gravity = Gravity.RIGHT;
			}
		}

		
		lp.topMargin = 20;
		mv.linearLayout.setLayoutParams(lp);
		mv.linearLayout.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				Bundle mBundle = new Bundle();
				SMS sms = new SMS();
				sms.setPhoneNum(num);
				sms.setBody(list.get(position).getBody());
				sms.setTime(list.get(position).getTime());
				sms.setType(list.get(position).getType());
				sms.setId(list.get(position).getId());
				sms.setThreadId(list.get(position).getThreadId());
				if (list.get(position).getType() == 1) {
					sms.setOperateType(3);
				} else if (list.get(position).getType() == 2) {
					sms.setOperateType(4);
				}
				mBundle.putParcelable("sms", sms);
				intent.putExtras(mBundle);
				intent.setClass(context, SmsActivity.class);
				context.startActivityForResult(intent, 0);
			}});
		

		convertView.setTag(mv);
		return convertView;
	}

	private static class MyView {
		LinearLayout linearLayout;
		TextView time;
		TextView content;
	}
}
